package dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import dao.IGoodsDAO;
import vo.GoodsInfo;

public class GoodsDAO extends BaseDAO implements IGoodsDAO {

	@Override
	public List getGoods(int page, int rows) {
		Session session = getSession();
		Query query = session.createQuery("from GoodsInfo c");
		int startRow = (page - 1) * rows;
		query.setFirstResult(startRow);
		query.setMaxResults(rows);
		List vip = query.list();
		session.close();
		return vip;

	}

	@Override
	public void addGoods(GoodsInfo goodsinfo) {
		// TODO Auto-generated method stub
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(goodsinfo);
		tx.commit();
		session.close();
	}

	@Override
	public void deleteGoods(GoodsInfo goodsinfo) {
		// TODO Auto-generated method stub
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.delete(goodsinfo);
		tx.commit();
		session.close();
	}

	@Override
	public void updateGoods(GoodsInfo goodsinfo) {
		// TODO Auto-generated method stub
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.update(goodsinfo);
		tx.commit();
		session.close();
	}

	@Override
	public GoodsInfo findGoodsById(int GId) {

		// TODO Auto-generated method stub
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		GoodsInfo goodsinfo = null;
		goodsinfo = (GoodsInfo) session.get(GoodsInfo.class, GId);
		tx.commit();
		session.close();
		return goodsinfo;
	}

	@Override
	public List searchGoods(String hql) {
		// TODO Auto-generated method stub

		Session session = getSession();
		Query query = session.createQuery(hql);
		List goods = query.list();
		session.close();
		return goods;

	}

	@Override
	public int getTotalNum() {
		// TODO Auto-generated method stub
		Session session = getSession();
		Query query = session.createQuery("from GoodsInfo c");
		List vip = query.list();
		int Num = vip.size();
		session.close();
		return Num;
	}
}
